/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.common.model;

import java.io.Serializable;
import java.util.List;

/**
 * 通用分页类
 * <p>用于从持久化层中获取数据
 *
 * @author yoara
 */
public class Pagination<T> implements Serializable {
    private static final long serialVersionUID = -2526095345442029659L;
    //默认当前页
    protected static final int DEFAULT_PAGE = 1;
    //默认页容量
    public static final int DEFAULT_PAGE_SIZE = 20;
    //查询结果集
    protected List<T> items;
    //结果总数
    protected int recordCount;
    //页容量
    protected int pageSize = DEFAULT_PAGE_SIZE;
    //当前页
    protected int currentPage = DEFAULT_PAGE;
    //是否查询总记录数
    private boolean queryRecordCount = true;

    public Pagination() {
        this(DEFAULT_PAGE_SIZE, DEFAULT_PAGE);
    }

    public Pagination(int pageSize, int page) {
        initPage(pageSize, page);
    }

    public Pagination(int pageSize, int page, boolean queryRecordCount) {
        initPage(pageSize < 1 ? DEFAULT_PAGE_SIZE : pageSize, page < 1 ? 1 : page);
        this.queryRecordCount = queryRecordCount;
    }

    private void initPage(int pageSize, int page) {
        this.pageSize = pageSize < 1 ? DEFAULT_PAGE_SIZE : pageSize;
        this.currentPage = page < 1 ? 1 : page;
    }

    public static boolean hasResults(Pagination<?> page) {
        if (null != page && null != page.getItems()
                && !page.getItems().isEmpty()) {
            return true;
        }
        return false;
    }

    public List<T> getItems() {
        return items;
    }

    public int getRecordCount() {
        return recordCount;
    }

    public int getCurrentPageStartIndex() {
        return (currentPage - 1) * pageSize;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }

    public void setRecordCount(int totalCount) {
        this.recordCount = totalCount;
    }

    public int getPageCount() {
        return (recordCount % pageSize == 0) ? (recordCount / pageSize)
                : (recordCount / pageSize) + 1;
    }

    public int getPreviousPage() {
        if (currentPage > 1) return currentPage - 1;
        else return DEFAULT_PAGE;
    }

    public int getNextPage() {
        if (currentPage < getPageCount()) return currentPage + 1;
        else return getPageCount();
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public boolean isQueryRecordCount() {
        return queryRecordCount;
    }

    public void setQueryRecordCount(boolean queryRecordCount) {
        this.queryRecordCount = queryRecordCount;
    }
}